package process0.visitor;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import process0.ast.Action;
import process0.ast.Process;
import process0.ast.States;
import process0.ast.Timeline;
import process0.ast.Workflow;

public class VisitorViewWorklowImpl implements Visitor {
	private String output;

	public void visit(Process node) {
		output = ";--------";
		for (Workflow workflow : node.getWorkflows()) {
			workflow.accept(this);
		}
	}

	public void visit(Workflow node) {
		output = output + "\n" + "[ticket-workflow]" + "\n;" + node.getId();
		for (Action action : node.getActions()) {
			action.accept(this);
		}
		writeToFile(node.getId(), output);
		ProcessBuilder pb = new ProcessBuilder("/bin/bash",
				"./script/showworkflow.sh", node.getId() + ".ini");
		try {
			pb.start();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void visit(Action node) {
		for (States state : node.getStates()) {
			state.accept(this);
		}
	}

	public void visit(States node) {
		String out = node.getActionId() + " = ";
		for (String string : node.getInitialStates()) {
			out += string + ", ";
		}
		out = out.substring(0, out.length() - 2);
		out = out + " -> " + node.getEndState();
		output = output + "\n" + out;
	}

	private void writeToFile(String filename, String output) {
		try {
			PrintWriter out = new PrintWriter(new FileWriter(filename + ".ini"));
			out.println(output);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void visit(Timeline node) {
		// TODO Auto-generated method stub
		
	}
}
